{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_initialization:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Biased Initialization\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One way of customizing an algorithm is a biased initial population. This can be very helpful if expert knowledge already exists, and known solutions should be improved. In the following, two different ways of initialization are provided: **a)** just providing the design space of the variables and **b)** a `Population` object where the objectives and constraints are provided and are not needed to be calculated again.\n",
    "\n",
    "**NOTE:** This works with all **population-based** algorithms in *pymoo*. Technically speaking, all algorithms which inherit from `GeneticAlgorithm`. For **local-search** based algorithm, the initial solution can be provided by setting `x0` instead of `sampling`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## By Array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from pymoo.algorithms.moo.nsga2 import NSGA2\n",
    "from pymoo.problems import get_problem\n",
    "from pymoo.optimize import minimize\n",
    "\n",
    "problem = get_problem(\"zdt2\")\n",
    "\n",
    "X = np.random.random((300, problem.n_var))\n",
    "\n",
    "algorithm = NSGA2(pop_size=100, sampling=X)\n",
    "\n",
    "minimize(problem,\n",
    "         algorithm,\n",
    "         ('n_gen', 10),\n",
    "         seed=1,\n",
    "         verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## By Population (pre-evaluated)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from pymoo.algorithms.moo.nsga2 import NSGA2\n",
    "from pymoo.problems import get_problem\n",
    "from pymoo.core.evaluator import Evaluator\n",
    "from pymoo.core.population import Population\n",
    "from pymoo.optimize import minimize\n",
    "\n",
    "problem = get_problem(\"zdt2\")\n",
    "\n",
    "# create initial data and set to the population object\n",
    "X = np.random.random((300, problem.n_var))\n",
    "pop = Population.new(\"X\", X)\n",
    "Evaluator().eval(problem, pop)\n",
    "\n",
    "algorithm = NSGA2(pop_size=100, sampling=pop)\n",
    "\n",
    "minimize(problem,\n",
    "         algorithm,\n",
    "         ('n_gen', 10),\n",
    "         seed=1,\n",
    "         verbose=True)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
